<?php
/**
 * 
 *
 * @author 
 * @version v1.0.0
 *
 **/
namespace Xijie\Controller;
use Think\Controller;

class RoomController extends Controller {
	public function __construct() {
        parent::__construct();
		//判断是否登陆
		if(session('user_id')){ //已登陆
			
		}else{ //未登陆，跳转道登陆界面
			$this->redirect("index/index");
		}
    }
	
	public function index(){
		if(IS_GET){
			$room_id = I('room_id') ? (int)I('room_id') : 0;
			if($room_id){
				$info = D('Room')->getRoomInfoById($room_id);
				$this->assign('info', $info);
				//获取规则
				$rentdata = D('Room')->getRoomRentByRoomNumber($info['room_number']);
				$this->assign('rentdata', $rentdata);
				$this->assign('attrcount', count($rentdata));
				$this->display('add');
			}else{
				$page = I('get.page') ? (int)I('get.page') : 1;
				$ajax = I('get.ajax') ? (int)I('get.ajax') : 0;
				$count = D('Room')->getRoomListCount();
				//用户列表
				$list = D('Room')->getRoomList($page);
				
				$this->assign('list', $list);
				$this->assign('count', $count);
				$this->assign('page', $page);
				
				//判断请求方式
				if(!$ajax){
					$this->display('index');
				}else{
					$this->display('room_list', 'utf-8', 'html');
				}
			}
		}else if(IS_POST){
			$data = array(
				'floor' => I('post.floor', 1),
				'room_number' => I('post.room_number') ? trim(I('post.room_number')) : '',
				'createtime' => time(),
				'rent' => I('post.rent') ? I('post.rent') : 0,
				'property' => I('post.property') ? I('post.property') : 0,
				'deposit' => I('post.deposit') ? I('post.deposit') : 0,
				'visible' => I('post.visible', 0),
				'root_type' => I('post.root_type', 'D1'),
				'rent_type' => I('post.rent_type', '长租'),
				'remark' => I('post.remark', '')
			);

			$room_id = I('post.room_id') ? (int)I('post.room_id') : 0;
			if(empty($data['room_number'])){
				$this->ajaxReturn('请输入房间号');
			}else{
				if($room_id){
					$row = D('Room')->editRoomInfo($room_id, $data);
				}else{
					$info = D('Room')->getRoomInfoByNumber($data['room_number']);
					if(is_array($info) && count($info)){
						$this->ajaxReturn('房间号已存在');
						exit;
					}else{
						$row = D('Room')->addRoomInfo($data);
					}
				}
				
				if($row !== false){
					//获取规则
					$param = I('post.param');
					
					if(is_array($param) && count($param)){
						foreach($param as $pm){
							if(!empty($pm['btime'])){
								$rentData[] = array(
									'roomnumber' => $data['room_number'],
									'begintime' => strtotime($pm['btime']. ' 00:00:00'),
									'endtime' => strtotime($pm['etime']. ' 23:59:59'),
									'rent' => $pm['rent'],
									'property' => $pm['property'],
									'deposit' => $pm['deposit'],
									'createtime' => time()
								);
							}
						}
						
						if(is_array($rentData) && count($rentData)){
							//删除原有的规则
							D('Room')->deleteRoomRentByRoomNumber($data['room_number']);
							//添加新规则
							D('Room')->addRoomRent($rentData);
						}
					}

					$this->ajaxReturn('操作成功');
				}else{
					$this->ajaxReturn('操作失败');
				}
			}
		}
	}
	
	public function add(){
		$this->assign('attrcount', 0);
		$this->display();
	}

	/**
	 * 删除
	 **/
	public function remove(){
		if(IS_POST){
			$id = I('post.id') ? (int)I('post.id') : 0;
			$row = D('Room')->delRoomInfo($id);
			if($row !== false){
				$this->ajaxReturn(1, 'json');
			}else{
				$this->ajaxReturn(0, 'json');
			}
		}
	}

	/**
	 * 房间办卡日志
	 **/
	public function roomcard(){
		if(IS_GET){
			$page = I('get.page') ? (int)I('get.page') : 1;
			$ajax = I('get.ajax') ? (int)I('get.ajax') : 0;
			
			//获取时间或者类型
			$param['room_id'] = I('get.room_id') ? (int)I('get.room_id') : 0;
			$param['btime'] = I('get.btime') ? trim(I('get.btime')) : '';
			$param['etime'] = I('get.etime') ? trim(I('get.etime')) : '';
			
			$where = array('card.visible' => 1);
			
			//付款类型 1：取卡 2：续费
			if($param['room_id']){
				$where['card.room_number'] = $param['room_id'];
			}
			
			//设置查询时间
			if(!empty($param['btime']) && !empty($param['etime'])){
				$btime = $param['btime']. '00:00:00';
				$etime = $param['etime']. '23:59:59';
				$where['card.checkin_time'] = array('egt', strtotime($btime));
				$where['card.leav_time'] = array('elt', strtotime($etime));
			}
			
			$count = D('Roomcard')->getRoomCardCount($where);
			//支付列表
			$list = D('Roomcard')->getRoomCardList($page, $where);
			
			$this->assign('list', $list);
			$this->assign('count', $count);
			$this->assign('page', $page);
			//设置查询参数
			$this->assign('param', json_encode($param));
			//判断请求方式
			if(!$ajax){
				$this->display('roomcard');
			}else{
				$this->display('roomcard_list', 'utf-8', 'html');
			}
		}else{ //退卡操作
			$id = I('post.id') ? (int)I('post.id') : 0;
			//获取办卡详细信息
			$cardinfo = D('Roomcard')->getRoomCarInfoByLogId($id);
			if(is_array($cardinfo) && count($cardinfo)){
				if($cardinfo['is_back']){
					echo '该卡已退';
				}else{
					//添加退卡日志
					$data = array(
						'log_id' => $cardinfo['id'],
						'roomnumber' => $cardinfo['room_number'],
						'member_id' => $cardinfo['member_id'],
						'nickname' => $cardinfo['member_nick'],
						'action_id' => 2,
						'createtime' => time(),
						'user_id' => session('user_id')
					);
					$row = D('Cardlog')->addCardlogInfo($data);
					if($row){
						//修改制卡记录状态
						D('Roomcard')->editRoomCardInfoByLogId($id);
						//添加退卡费用日志
						$userinfo = D('Member')->getMemberInfoById($cardinfo['member_id']);
						
						$goback_data = array(
							'typeid' => 0,
							'contract' => $userinfo['contract'],
							'userid' => $cardinfo['member_id'],
							'nickname' => $userinfo['member_nick'],
							'roomnumber' => $cardinfo['room_number'],
							'card_deposit' => '-100.00',
							'acount' => '-100.00',
							'xianjin' => '-100.00',
							'pay_type' => 0,
							'createtime' => time(),
							'admin_nick' => session('nickname'),
							'remark' => '退卡押金',
							'status' => 1
						);
						$row = D('Gobacklog')->addGobacklogInfo($goback_data);

						echo 1;
					}else{
						echo '退卡失败';
					}
				}
			}else{
				echo '数据无效';
			}
		}
	}
	
	/**
	 * 类型描述
	 **/
	public function profile(){
		if(IS_GET){
			$info = M('room_profile')->where(array(
				'room_type' => 'D1'
			))->find();
			$this->assign('info', $info);
			$this->assign('room_type', 'D1');
			$this->display();
		}else{
			$inputData = I('post.');
			$inputData['admin_id'] = session('user_id');
			$inputData['admin_nick'] = session('nickname');
			$inputData['createtime'] = time();

			$room_type = $inputData['room_type'];
			//判断是否存在数据
			$count = M('room_profile')->where(array(
				'room_type' => $room_type
			))->count();
			//开始判断逻辑
			if($count){
				if(empty($inputData['room_images'])){
					unset($inputData['room_images']);
				}
				
				$row = M('room_profile')->where(array(
					'room_type' => $room_type
				))->setField($inputData);
			}else{
				$row = M('room_profile')->data($inputData)->add();
			}

			if($row !== false){
				echo 1;
			}else{
				echo 0;
			}
		}
	}

	public function ajaxprofile(){
		$room_type = I('get.room_type', 'D1');
		$info = M('room_profile')->where(array(
			'room_type' => $room_type
		))->find();
		
		if(count($info)){
			$array = array(
				'errCode' => 1,
				'info' => $info
			);
		}else{
			$array = array(
				'errCode' => 0
			);
		}

		echo json_encode($array);

	}

	/**
	 * 上传图片
	 **/
	public function upload(){
		if(!empty($_FILES['file']['tmp_name'])){
		    $upload = new \Think\Upload();// 实例化上传类
		    $upload->maxSize   =     3145728 ;// 设置附件上传大小
		    $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		    $upload->rootPath  =     './Public/upload/'; // 设置附件上传根目录
		    $upload->savePath  =     ''; // 设置附件上传（子）目录
		    // 上传文件 
		    $info   =   $upload->upload();
		    if(!$info) {// 上传错误提示错误信息
		        $data = array('errCode' => 0, 'errMsg' => $upload->getError());
		    }else{// 上传成功
		        foreach($info as $file){
			        $newImage[] = $file['savepath'].$file['savename'];
			    }
				
				$data = array('errCode' => 1, 'errMsg' => serialize($newImage));
		    }
	    }else{
	    	$data = array('errCode' => 0, 'errMsg' => '请选择上传的图片');
	    }
		
		echo json_encode($data);
	}
}


   